package com.baibei.ebec.user.reset;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.baibei.basic.IPresenter;
import com.baibei.basic.module.utils.ViewExtCompat;
import com.baibei.ebec.user.R;
import com.baibei.ebec.user.R2;
import com.baibei.ebec.user.register.TextViewCompat;
import com.baibei.module.AppRouter;
import com.baibei.module.BasicActivity;
import com.baibei.ui.AppUI;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 登录
 * Created by ChenRui on 2017/5/3 0003 11:41.
 */
@Route(path = AppRouter.ROUTER_USER_RESET_PASSWORD)
public class RestPasswordActivity extends BasicActivity implements ResetPasswordContract.View {

    @BindView(R2.id.et_phone)
    EditText mPhoneText;

    @BindView(R2.id.et_password)
    EditText mPasswordText;

    @BindView(R2.id.img_edit_delete)
    ImageView mEditDeleteView;
    @BindView(R2.id.btn_reset)
    Button mResetButton;
    @BindView(R2.id.cb_eyes)
    CheckBox mEyes;

    @BindView(R2.id.btn_send_sms)
    TextView mSendSMSView;
    @BindView(R2.id.et_sms_code)
    EditText mSmsCodeText;
    private ResetPasswordContract.Presenter mPresenter;
    private TextWatcher mTextWatcher;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reset_password);
        mPresenter = inject(ResetPasswordContract.Presenter.class);


        ViewExtCompat.relevancePasswordVisibility(mEyes, mPasswordText);
        mTextWatcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (mPhoneText.length() >= 11 && mPasswordText.length() >= 6 && mSmsCodeText.length() >= 4) {
                    mResetButton.setEnabled(true);
                } else {
                    mResetButton.setEnabled(false);
                }
                mEditDeleteView.setVisibility(mPhoneText.length() > 0 ? View.VISIBLE : View.GONE);
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        };

        TextViewCompat.registerFormListener(mTextWatcher, mPhoneText, mPasswordText, mSmsCodeText);
    }

    @Override
    protected void onDestroy() {
        TextViewCompat.unregisterFormListener(mTextWatcher, mPhoneText, mPasswordText, mSmsCodeText);
        super.onDestroy();
    }

    @Override
    public IPresenter getBasicPresenter() {
        return mPresenter;
    }

    @OnClick(R2.id.btn_reset)
    public void onResetClick() {
        AppUI.loading(this, "正在重置密码..");
        mResetButton.setEnabled(false);
        mPresenter.resetPassword();
    }


    @OnClick(R2.id.img_edit_delete)
    public void onEditDeleteClick() {
        mPhoneText.setText("");
    }

    @OnClick(R2.id.btn_send_sms)
    public void onSendSmsClick() {
        mSendSMSView.setEnabled(false);
        mPresenter.sendSms();
    }

    @Override
    public String getPhoneNumber() {
        return mPhoneText.getText().toString();
    }

    @Override
    public void onResetPasswordFailed(String message) {
        AppUI.dismiss();
        AppUI.failed(this, message);
        mResetButton.setEnabled(true);
    }

    @Override
    public void onResetPasswordSuccess() {
        mResetButton.setEnabled(true);
        AppUI.dismiss();
        AppUI.success(this, "重置密码成功");
        AppRouter.routeToLogin(this);
        finish();
    }

    @Override
    public String getPassword() {
        return mPasswordText.getText().toString();
    }

    @Override
    public String getSmsCode() {
        return mSmsCodeText.getText().toString();
    }

    @Override
    public void onSmsSuccess() {
        AppUI.dismiss();
        AppUI.success(this, getString(R.string.send_sms_success));
    }

    @Override
    public void onSmsFailed(String msg) {
        AppUI.dismiss();
        AppUI.failed(this, msg);
        mSendSMSView.setEnabled(true);
        mSendSMSView.setText(R.string.send_sms);
    }

    @Override
    public void onSmsCountDown(int i, String text) {
        mSendSMSView.setText(text);
    }

    @Override
    public void onSmsCountDownFinish() {
        mSendSMSView.setEnabled(true);
        mSendSMSView.setText(R.string.send_sms);
    }
}
